import 'dart:io' show Platform;

import 'package:flutter/services.dart';
import 'package:flutter_permissions/flutter_permissions.dart';
import 'package:flutter_permissions/flutter_permissions_platform_interface.dart';

import 'flutter_utils_platform.dart';

const MethodChannel _channel = const MethodChannel('com.flutter.hlibrary/utils');

class MethodFlutterUtils extends FlutterUtilsPlatform {
  @override
  Future<String> getCurrentNetworkType() async {
    if (Platform.isAndroid == true) {
      return await _channel.invokeMethod("getCurrentNetworkType");
    } else if (Platform.isIOS == true) {
      return "4G";
    }
    return "";
  }

  @override
  Future<String> getImei() async {
    if (Platform.isAndroid == true) {
      PermissionStatus permissionStatus =
          await FlutterPermissions.requestPermissions(
              [Permission.ReadPhoneState]);
      if (permissionStatus == PermissionStatus.authorized) {
        return await _channel.invokeMethod("getImei");
      }
    }
    return "";
  }

  @override
  Future<String> getLanguage() async {
    return await _channel.invokeMethod("getLanguage");
  }

  @override
  Future<int> getPhoneType() async {
    PermissionStatus permissionStatus =
        await FlutterPermissions.requestPermission(Permission.ReadPhoneState);
    if (permissionStatus == PermissionStatus.authorized) {
      final int result = await _channel.invokeMethod("getPhoneType");
      return result;
    }
    return -1;
  }

  @override
  Future<bool> haveExternalStorage() async {
    if (Platform.isAndroid == true) {
      PermissionStatus permissionStatus =
          await FlutterPermissions.requestPermissions([
        Permission.WriteExternalStorage,
        Permission.ReadExternalStorage
      ]);
      if (permissionStatus == PermissionStatus.authorized) {
        final bool result = await _channel.invokeMethod("haveExternalStorage");
        return result;
      }
      return false;
    } else if (Platform.isIOS == true) {
      return true;
    }
    return false;
  }

  @override
  Future<double> getScreenHeight() async{
    final double result = await _channel.invokeMethod("getScreenHeight");
    return result;
  }

  @override
  Future<double> getScreenRatio() async{
    final double result = await _channel.invokeMethod("getScreenRatio");
    return result;
  }

  @override
  Future<double> getScreenWidth() async{
    final double result = await _channel.invokeMethod("getScreenWidth");
    return result;
  }
}
